<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col :span=6>
                        <el-form-item label="助记名称" prop="name">
                            <el-input v-model="condition.search.name"
                                      placeholder="助记名称"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span=6>
                        <el-form-item label="配置ID" prop="configId">
                            <el-input v-model="condition.search.configId"
                                      placeholder="配置ID"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" @click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="showAddDialog">新增</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="reset">重置</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="exportExcel">导出</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            size="small"
            :data="tableData"
            @selection-change="handleSelectionChange"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column type="selection" width="55" v-if="mode === 'select'"></el-table-column>
            <el-table-column label="序号" width="60">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="name"
                label="助记名称">
            </el-table-column>
            <el-table-column
                prop="configId"
                label="配置ID">
            </el-table-column>
            <el-table-column
                prop="qrCode"
                label="二维码">
                <template v-slot="scope">
                    <el-image style="width: 80px; height: 80px"
                              v-if="scope.row.scene == 2"
                              :src="scope.row.qrCode"
                              :preview-src-list="qrcodeImgUrls"
                              fit="contain">
                    </el-image>
                    <span v-else>
                        {{ scope.row.qrCode }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column
                prop="scene"
                :formatter="formatScene"
                label="场景">
            </el-table-column>
            <el-table-column
                prop="autoCreateRoom"
                :formatter="formatAutoCreateRoom"
                label="群满后是否自动新建群">
            </el-table-column>
            <el-table-column
                prop="roomBaseName"
                label="自动建群的群名前缀">
            </el-table-column>
            <el-table-column
                prop="roomBaseId"
                label="自动建群的群起始序号">
            </el-table-column>
            <el-table-column
                prop="groupChatNames"
                label="关联群">
            </el-table-column>
            <el-table-column
                prop="state"
                label="自定义参数">
            </el-table-column>
            <el-table-column
                prop="createTime"
                label="创建时间">
            </el-table-column>
            <el-table-column
                prop="updateTime"
                label="更新时间">
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                align="center"
                width="80px">
                <template v-slot="scope">
                    <div>
                        <el-button @click="showUpdateDialog(scope.row)" type="text" size="small">编辑</el-button>
                        <el-dropdown>
                            <el-button type="text" size="small">
                                更多
                            </el-button>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item>
                                    <el-button @click="config(scope.row)" type="text" size="small">配置</el-button>
                                </el-dropdown-item>
                                <el-dropdown-item>
                                    <el-button @click="onDelete(scope.row)" type="text" size="small">删除</el-button>
                                </el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>
        <el-dialog :title=addOrUpdateDialogTitle width="30%" :visible.sync="addOrUpdateDialogVisible">
            <el-form :model="addOrUpdateForm" label-width="160px" size="small">
                <el-form-item label="助记名称">
                    <el-input v-model="addOrUpdateForm.name"
                              placeholder="助记名称"></el-input>
                </el-form-item>
                <el-form-item label="场景">
                    <el-select v-model="addOrUpdateForm.scene" :disabled="disabled"
                               placeholder="场景">
                        <el-option label="二维码" :value="2"></el-option>
                        <el-option label="小程序" :value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="群满后是否自动新建群">
                    <el-switch v-model="addOrUpdateForm.autoCreateRoom"></el-switch>
                </el-form-item>
                <el-form-item label="自动建群的群名前缀" v-if="addOrUpdateForm.autoCreateRoom">
                    <el-input v-model="addOrUpdateForm.roomBaseName"
                              placeholder="自动建群的群名前缀"></el-input>
                </el-form-item>
                <el-form-item label="自动建群的群起始序号" v-if="addOrUpdateForm.autoCreateRoom">
                    <el-input v-model="addOrUpdateForm.roomBaseId"
                              placeholder="自动建群的群起始序号"
                              oninput="value=value.replace(/[^\d]/g,'')"></el-input>
                </el-form-item>
                <el-form-item label="选择群">
                    <el-button @click="selectGroupChatDialogVisible = true" size="mini" type="plain"
                               v-if="selectedGroupChats.length === 0">
                        点击选择
                    </el-button>
                    <el-button @click="selectGroupChatDialogVisible = true" size="mini" type="success" v-else>
                        已选择{{ selectedGroupChats.length }}个群
                    </el-button>
                    <div class="form-tips">最多选择5个群哦</div>
                </el-form-item>
                <el-form-item label="自定义参数">
                    <el-input v-model="addOrUpdateForm.state"
                              placeholder="自定义参数"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addOrUpdateDialogVisible = false" size="small">取 消</el-button>
                <el-button @click="addOrUpdateConfirm" type="primary" size="small">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="选择群" width="50%" :visible.sync="selectGroupChatDialogVisible" append-to-body>
            <work-group-chat ref="groupChat" size="small" mode="select" :work-corp-id="workCorpId" :selected="selectedGroupChats"></work-group-chat>
            <div slot="footer" class="dialog-footer">
                <el-button @click="selectGroupChatDialogVisible = false" :size="size">取 消</el-button>
                <el-button @click="selectGroupChatConfirm" type="primary" :size="size">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="配置" width="50%" :visible.sync="configDialogVisible">
            <div slot="footer" class="dialog-footer">
                <el-button @click="configDialogVisible = false" :size="size">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addWorkQrcodeGroupChat,
    deleteWorkQrcodeGroupChat,
    getWorkQrcodeGroupChatByPage,
    updateWorkQrcodeGroupChat
} from '@/api/api'
import WorkGroupChat from "@/component/workwx/WorkGroupChat";

export default {
    name: "WorkQrcodeGroupChat",
    components: {WorkGroupChat},
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {},
            // 禁用
            disabled: false,

            qrcodeSelected: [],

            // 群选择
            selectGroupChatDialogVisible: false,
            selectedGroupChats: [],

            configDialogVisible: false,

            // 二维码图片数组
            qrcodeImgUrls: [],
        }
    },
    // 这里定义需要父组件传过来的东西
    props: {
        size: {
            type: String,
            default: 'small'
        },
        mode: {
            type: String,
            default: 'list'
        },
        // 选中的行
        selected: {
            type: Array,
            default: () => []
        },
        workCorpId: String
    },
    methods: {
        handleSelectionChange(val) {
            this.qrcodeSelected = val;
        },
        selectGroupChatConfirm() {
            this.selectedGroupChats = this.$refs.groupChat.groupChatSelected;
            this.selectGroupChatDialogVisible = false;
            this.addOrUpdateForm.groupChats = this.selectedGroupChats;
        },
        config() {
            this.configDialogVisible = true;
        },
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        formatScene: function (row, column, cellValue) {
            if (cellValue == '1') {
                return "小程序";
            } else if (cellValue == '2') {
                return "二维码";
            } else {
                return "未知";
            }
        },
        formatAutoCreateRoom: function (row, column, cellValue) {
            if (cellValue == '0') {
                return "否";
            } else if (cellValue == '1') {
                return "是";
            } else {
                return "未知";
            }
        },
        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
            this.addOrUpdateForm.workCorpId = this.workCorpId;
            this.disabled = false;
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
            this.disabled = true;
            if (row.groupChats) {
                this.selectedGroupChats = row.groupChats;
            } else {
                this.selectedGroupChats = [];
            }
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addWorkQrcodeGroupChat(this.addOrUpdateForm).then(data => {
                if (data.respCode == 1000) {
                    this.addOrUpdateDialogVisible = false;
                    this.getData();
                }
            });
        },
        update: function () {
            updateWorkQrcodeGroupChat(this.addOrUpdateForm).then(data => {
                if (data.respCode == 1000) {
                    this.addOrUpdateDialogVisible = false;
                    this.getData();
                }
            });
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteWorkQrcodeGroupChat({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取表格数据
        getData: function () {
            if (this.workCorpId) {
                this.condition.search.workCorpId = this.workCorpId;
            }
            getWorkQrcodeGroupChatByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;

                this.tableData.forEach((item, index, arr) => {
                    if (item.scene == 2) {
                        this.qrcodeImgUrls.push(item.qrCode);
                    }
                })
            });
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
